package com.xhj.springbootproject.service.impl;

import com.xhj.springbootproject.service.intf.SendMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author ：Xionghaijun
 * @date ：Created in 2019/7/24 10:14 AM
 * @description：
 */
@Service
public class SendMailServiceImpl implements SendMailService {

    Logger logger = LoggerFactory.getLogger(SendMailServiceImpl.class);

    @Value("${spring.mail.username}")
    private String whoAmI;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送简单邮件
     * @param to 发送给谁
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(whoAmI);
        mailSender.send(message);
        Integer.toBinaryString(10);
    }

    /**
     * 发送html邮件
     * @param to 发送给谁
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setTo(to);
            helper.setFrom(whoAmI);
            helper.setSubject(subject);
            helper.setText(content,true);
            mailSender.send(message);
        } catch (Exception e) {
            logger.error("发送html邮件出错，错误信息 {}", e);
        }
    }

    /**
     * 发送附件邮件
     * @param to 发送给谁
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param filePath 附件路径
     */
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setTo(to);
            helper.setFrom(whoAmI);
            helper.setSubject(subject);
            helper.setText(content,true);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            //此处可以添加多个附件 zjy0910
            helper.addAttachment(fileName,file);

            mailSender.send(message);
        } catch (Exception e) {
            logger.error("发送html邮件出错，错误信息 {}", e);
        }
    }

    /**
     * 发送图片邮件
     * @param to 发送给谁
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param rscPath 图片路径
     * @param rscId img标签中的src地址
     */
    @Override
    public void sendInlinResourceMail(String to, String subject, String content, String rscPath, String rscId) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setFrom(whoAmI);
            helper.setSubject(subject);
            helper.setText(content,true);

            //可以添加多个图片
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId,res);

            mailSender.send(message);
        } catch (Exception e) {
            logger.error("发送html邮件出错，错误信息 {}", e);
        }
    }
}
